package workflow

import (
	"encoding/xml"
	"io/ioutil"
	glog "log"
	"strings"

	"shqsoft.cn/pasnj/base"

	//使用mssql数据库
	_ "github.com/jinzhu/gorm/dialects/mssql"
)

//Context 配置上下文
type Context struct {
	XMLName   xml.Name   `xml:"WorkFlows"`
	WorkFlows []WorkFlow `xml:"WorkFlow"`
}

//GetWorkFlow 获取流程定义
func GetWorkFlow(id int) *WorkFlow {
	for _, wf := range wfContext.WorkFlows {
		if wf.ID == id {
			return &wf
		}
	}
	return nil
}

//WorkFlow WorkFlow参数
type WorkFlow struct {
	ID       int      `xml:"id,attr"`
	Name     string   `xml:"name,attr"`
	Actions  []Action `xml:"Action"`
	Entity   string   `xml:"entity,attr"`
	Recorder string   `xml:"recorder,attr"`
	PermID   int      `xml:"permID,attr"`
	Manager  string   `xml:"manager,attr"`
}

//Action Action
type Action struct {
	Step          int       `xml:"step,attr"`
	Desc          string    `xml:"desc,attr"`
	Executor      string    `xml:"executor,attr"`
	Appointer     string    `xml:"appointer,attr"`
	OrganFrom     string    `xml:"organFrom,attr"`
	IsAuto        bool      `xml:"isAuto,attr"`
	ConSign       bool      `xml:"conSign,attr"`
	CanBack       bool      `xml:"canBack,attr"`
	End           bool      `xml:"end,attr"`
	MustDesc      bool      `xml:"mustDesc,attr"`
	Excludes      []Exclude `xml:"Exclude"`
	CanManager    bool      `xml:"canManager,attr"`
	IsSecondLevel bool      `xml:"isSecondLevel,attr"`
}

type Exclude struct {
	ID   string `xml:"id,attr"`
	Func string `xml:"func,attr"`
}

//GetAction 根据step获取Action
func (wf *WorkFlow) GetAction(step int) *Action {
	for _, act := range wf.Actions {
		if act.Step == step {
			return &act
		}
	}
	return nil
}

func (wf *WorkFlow) IsManager(userId int) (isMg bool) {
	if wf.Manager != "" {
		mArr := strings.Split(wf.Manager, ",")
		sql := "select COUNT(*) from t_UserRole WHERE USERID=? AND ROLEID IN (?)"
		row := base.DB.Raw(sql, userId, mArr).Row()
		var count int
		row.Scan(&count)
		if count > 0 {
			isMg = true
		}
	}
	return
}

//WFContext 工作流配置信息
var wfContext Context

func init() {
	var err error
	var workPath string
	if workPath, err = base.WorkDir(); err != nil {
		glog.Printf("获取执行路径出错！%s", err)
		return
	}
	filePath := workPath + "/config/workflow.xml"
	if xmlBytes, err := ioutil.ReadFile(filePath); err == nil {
		if err := xml.Unmarshal(xmlBytes, &wfContext); err != nil {
			glog.Printf("加载WorkFlow配置文件失败！%s", err)
		}
	}
	for _, wf := range wfContext.WorkFlows {
		alen := len(wf.Actions)
		for i := 0; i < alen; i++ {
			act := wf.Actions[i]
			if act.Step != i+1 {
				glog.Printf("流程%s配置错误，每个活动的step必须从1开始，并且连续", err)
				break
			}
			if i == alen-1 {
				wf.Actions[i].End = true
			}
		}
	}
}
